﻿using System;
using UnityEngine;

namespace Engine.UI
{
    /// <summary>
    /// 面板基类
    /// </summary>
    public abstract class ViewBase : MonoBehaviour
    {
        private bool DataInvalid = false;
        public Transform tPrefab = null;

        /// <summary>
        /// 获取资源文件名
        /// </summary>
        /// <returns></returns>
        public virtual string GetPath()
        {
            throw new Exception();
        }

        public virtual void OnLoaded(Transform prefab)
        {
            this.tPrefab = prefab;
            AddEvents();
        }

        public virtual void AddEvents()
        {

        }

        public virtual void LateUpdate()
        {
            if (DataInvalid && tPrefab != null)
            {
                DataInvalid = false;
                DataUpdate();
            }
        }

        public virtual void DataUpdate()
        {

        }

        public void InvalidData()
        {
            DataInvalid = true;
        }


        public virtual void RemoveEvents()
        {

        }
        public virtual void Show()
        {
            if (tPrefab != null)
            {
                tPrefab.gameObject.SetActive(true);
            }
        }

        /// <summary>
        /// 关闭面板
        /// </summary>
        public virtual void Close()
        {
            UIManager.Instance.Close(this);
        }

        public virtual void Close(float t)
        {
            Invoke("Close", t);
        }

        public virtual void OnDestroy()
        {
            RemoveEvents();
        }
    }
}